/*
 * Ortho.java
 *
 * Created on April 30, 2007, 9:34 PM
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.graphics.ortho;

import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.LightState;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.TextureState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.GameTaskQueueManager;
import com.jme.util.TextureManager;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ConcurrentModificationException;
import java.util.concurrent.Callable;
import kanjitori.graphics.AbstractThing;
import kanjitori.util.GraphicUtil;

/**
 *
 * @author DGronau
 */
public class Ortho extends AbstractThing {
    
    protected Quad getQuad(String name, Rectangle pos) {
        Quad quad = new Quad(name, pos.width, -pos.height); //else strings are flipped
        quad.setLocalTranslation(new Vector3f(pos.x + pos.width/2, pos.y - pos.height/2, 0));
        quad.setLightCombineMode(LightState.OFF);
        quad.setRenderQueueMode(display().getRenderer().QUEUE_ORTHO);
        quad.setRenderState(GraphicUtil.getTransparence());
        quad.setRenderState(GraphicUtil.getZState());
        quad.updateRenderState();
        return quad;
    }
    
    protected void setBufferedImage(final BufferedImage bi, final Quad quad) {
        GameTaskQueueManager.getManager().update(new Callable<Object>() {
            public Object call() throws Exception {
                try {
                    TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
                    Texture t = TextureManager.loadTexture(bi, Texture.MM_LINEAR, Texture.MM_LINEAR, 1, false);
                    ts.setTexture(t);
                    quad.setRenderState(ts);
                    quad.updateRenderState();
                    TextureState oldTs = (TextureState)quad.getRenderState(RenderState.RS_TEXTURE);
                    if (oldTs != null) {
                        TextureManager.releaseTexture(oldTs.getTexture());
                        oldTs.deleteAll(true);
                    }
                } catch (ConcurrentModificationException ex) {
                    ex.printStackTrace();
                }
                return null;
            }
        });
    }
    
}
